#include<iostream>
#include<vector>
using namespace std;

vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {
    vector<int> result;
    int flag = 0;
    for(int i=0;i<queries.size();i++)
    {
        int max,min;
        if(queries[i][0]==queries[i][1])
        {
            result.push_back(queries[i][0]);
            continue;
        }
        else if(queries[i][0]>queries[i][1])
        {
            max = queries[i][0];
            min = queries[i][1];
        }
        else
        {
            max = queries[i][1];
            min = queries[i][0];
        }
        if(heights[min] < heights[max])
        {
            result.push_back(max);
            flag = 1;
        } 
        else
        {
            for(int j=max;j<heights.size();j++)
            {
                if(heights[j]>heights[min]&&heights[j]>heights[max])
                {
                    result.push_back(j);
                    flag = 1;
                    break;
                }
            }
        }
        cout<<flag<<endl;
        if(!flag)
        {
            result.push_back(-1);
        }
        flag = 0;
    }
    return result;    
}
int main()
{
    vector<int> heights;
    vector<vector<int>> queries(5);
    cout<<"enter heights"<<endl;
    for(int i=0;i<6;i++)
    {
        int temp;
        cin>>temp;
        heights.push_back(temp);
    }
    cout<<"enter queries"<<endl;
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<2;j++)
        {
            int temp;
            cin>>temp;
            queries[i].push_back(temp);
        }
    }
    vector<int> result = leftmostBuildingQueries(heights,queries);
    cout<<"result is"<<endl;
    for(int i=0;i<result.size();i++)
    {
        cout<<result[i]<<" ";
    }
    return 0;
}